package br.uniriotec.ppgi.leo.view;

import java.util.Iterator;

import org.apache.log4j.Logger;

import br.uniriotec.ppgi.leo.controller.run.LEOProcessor;

import com.martiansoftware.jsap.FlaggedOption;
import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.Switch;

public class CommandLineRunner {
	
	private static Logger logger = Logger.getLogger(CommandLineRunner.class);
	
	public static void main(String[] args) throws Exception{

		/* ************************************************
		 * 	Parse Parameters from command line with JSAP
		 * ************************************************/
		JSAPResult parameters = parseParameters(args);
		String filepath = parameters.getString("file");
		String lth_srl_root = parameters.getString("lth_srl_root");
		
		logger.info("## STARTING LEO (Learn Ontologies through NLP)");
		
		/* ************************************************
		 * 				Start application
		 * ************************************************/
		LEOProcessor processor = new LEOProcessor();
		processor.process(filepath, lth_srl_root);
		
		logger.info("## FINISHING LEO");
		
	}
	
	
	
	
	
	
	
	
	
	@SuppressWarnings("rawtypes")
	private static JSAPResult parseParameters(String[] args) throws Exception{
		
		JSAP jsap = new JSAP();
		
		
		/* ****************************************
		 * 		Create Paramenter options
		 * ****************************************/
		
        FlaggedOption optFilename = new FlaggedOption("file")
	        .setRequired(true) 
	        .setShortFlag('f') 
	        .setLongFlag("file");
	    	optFilename.setHelp("The path to the input TXT file with domain description.");
	    	
    	FlaggedOption lthRoot = new FlaggedOption("lth_srl_root")
	        .setRequired(true) 
	        .setShortFlag('l') 
	        .setLongFlag("lth_srl_root");
	    	optFilename.setHelp("The path LTH_SRL root where models and descriptors folders are.");
        
        Switch swVerbose = new Switch("verbose")
			.setShortFlag('v')
			.setLongFlag("verbose");
			swVerbose.setHelp("Outputs verbose info.");
        
        Switch swHelp = new Switch("help")
			.setShortFlag('h')
			.setLongFlag("help");
			swHelp.setHelp("Shows this help info.");
		
			
			
			
		/* ****************************************
		 * 			Register Parameters
		 * ****************************************/	
		
		try {
			jsap.registerParameter(optFilename);
			jsap.registerParameter(lthRoot);
			jsap.registerParameter(swVerbose);
			jsap.registerParameter(swHelp);
		} catch (Exception e) {
			throw new Exception("FATAL Exception when registering JSAP Parameters. "
					+ e.getMessage());
		}
		

		
		/* ****************************************
		 * 			Parser arguments
		 * ****************************************/
		
        JSAPResult config = jsap.parse(args);  
        
        //Check if its a call for Help
        if(config.getBoolean("help")){
			System.err.println(jsap.getHelp());
			System.exit(1);
			
		//Check if its an usage mistake	
        }else if(!config.success()) {
        	for (Iterator errs = config.getErrorMessageIterator(); errs.hasNext();) {
                System.err.println("Error: " + errs.next());
            }    
        	System.err.println("Usage: java -jar LEO-{version}.jar "+jsap.getUsage());
        	System.exit(1);
        
        //Otherwise, return the parsed parameters 
        }else{
        	return config; 
			
        }
        
        return null;
        
	}

}
